package com.yenlien.traceyun.plant.controller;

import com.yenlien.traceyun.plant.service.DockDeviceService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author jialong
 * @date 2021/03/31
 * @description: 新风向和科百接口
 **/

@RestController
@RequestMapping("/dockDevice")
public class DockDeviceController extends BaseController {

    @Autowired
    private DockDeviceService dockDeviceService;


    /**
     * 根据点的ID获取该点绑定设备3天前至现在的数据,每天各一组数据
     *
     * @param baseSoilId
     * @return
     */
    @GetMapping("/findList3Days")
    public Object findList3Days(String baseSoilId) {
        if (StringUtils.isNotBlank(baseSoilId)) {
            return success(dockDeviceService.findList3Days(baseSoilId));
        }
        return success(new ArrayList<>());
    }


    /**
     * 实时获取新风向和科百的数据
     *
     * @param baseSoilId
     * @return
     */
    @GetMapping("/getNowData")
    public Object getNowData(String baseSoilId) {
        if (StringUtils.isNotBlank(baseSoilId)) {
            return success(dockDeviceService.getNowData(baseSoilId));
        }
        return success(new ArrayList<>());
    }

    /**
     * 根据点的ID获取对应摄像头的播放地址
     *
     * @param baseSoilId
     * @return
     */
    @GetMapping("/getUrl")
    public Object getUrl(String baseSoilId) {
        if (StringUtils.isNotBlank(baseSoilId)) {
            return success(dockDeviceService.getUrl(baseSoilId));
        }
        return success(new ArrayList<>());
    }


    /**
     * 根据时间区间和设备ID查询历史数据并按传感器类型分组
     *
     * @param startTime
     * @param endTime
     * @param equId
     * @return
     */
    @GetMapping("/getHistoryData")
    public Object getHistoryData(String startTime, String endTime, @RequestParam("equId") String equId) {
        if (startTime != null && endTime != null) {
            return dockDeviceService.getHistoryData(new Date(Long.valueOf(startTime)), new Date(Long.valueOf(endTime)), equId);
        } else {
            return dockDeviceService.getHistoryData(Date.from(LocalDateTime.now().minusDays(7).atZone(ZoneId.systemDefault()).toInstant()), new Date(), equId);
        }
    }

    /**
     * 根据地块查询该地块下的所有设备
     *
     * @param massifId
     * @return
     */
    @GetMapping("/getEquByMassifId")
    public Object getEquByMassifId(@RequestParam("massifId") String massifId, String type) {
        return dockDeviceService.getEquByMassifId(massifId, type);
    }


}
